package com.mdc.mansion.core.modules.restaurant.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.restaurant.RestaurantConfigService;
import com.mdc.mansion.core.modules.restaurant.RestaurantExtService;
import com.mdc.mansion.core.modules.restaurant.RestaurantService;
import com.mdc.mansion.core.modules.restaurant.RestaurantStaffService;
import com.mdc.mansion.core.modules.restaurant.dto.RestaurantDTO;
import com.mdc.mansion.core.modules.system.SysDictService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.restaurant.dao.RestaurantDao;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantExt;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantStaff;
import com.mdc.mansion.dataaccess.modules.system.entity.User;

/**
 * 餐厅信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class RestaurantServiceImpl extends CrudService<RestaurantDao, Restaurant> implements RestaurantService {
	@Autowired
    private RestaurantStaffService restaurantStaffService;
    @Autowired
    private RestaurantExtService restaurantExtService;
    @Autowired
    private RestaurantConfigService restaurantConfigService;
    @Autowired
    private SysDictService sysDictService;

    @Override
    public RestaurantDTO currentUserRestaurant() {
        Restaurant restaurant = ContextHolder.currentRestaurant();
        RestaurantDTO restaurantDTO=new RestaurantDTO();
        /**
         * 添加手机号码拆分
         */
        if("+".equals(restaurant.getTel().charAt(0)))
        {
        	restaurantDTO.setNoHead(restaurant.getTel().substring(0,3));
            restaurantDTO.setTel(restaurant.getTel().substring(3, restaurant.getTel().length()-4));
        }
        
        BeanUtils.copyProperties(restaurant,restaurantDTO);
        List<RestaurantExt> restaurantExts=restaurantExtService.findImgList(restaurant.getId());
        restaurantDTO.addImgExtMap(restaurantExts);
        return restaurantDTO;
    }

    @Override
    @Transactional
    public void saveCurrentUserRestaurant(RestaurantDTO restaurantDTO) {
        RestaurantStaff restaurantStaff = ContextHolder.currentRestaurantStaff();
        restaurantDTO.setChineseName(restaurantDTO.getName());
        restaurantDTO.setEnglishName(restaurantDTO.getName());
        restaurantDTO.setChineseAddress(restaurantDTO.getChineseAddress());
        restaurantDTO.setEnglishAddress(restaurantDTO.getEnglishAddress());
        /**
         * 添加手机号码合并
         */
        restaurantDTO.setTel(restaurantDTO.getNoHead()+restaurantDTO.getTel());
        
        restaurantDTO.verfiy();
        if(StringUtils.isBlank(restaurantStaff.getRestaurantId())){
            this.createCurrentUserRestaurant(restaurantDTO,restaurantStaff);
        }else{
            this.updateCurrentUserRestaurant(restaurantDTO,restaurantStaff);
        }
    }
    @Transactional
    protected void createCurrentUserRestaurant(RestaurantDTO restaurantDTO, RestaurantStaff restaurantStaff) {
        restaurantDTO.verfiy();
        Restaurant restaurant=new Restaurant();
        BeanUtils.copyProperties(restaurantDTO,restaurant);
        User currentOperationUser = RestaurantSupport.getCurrentOperationUser();
        restaurant.setCreateBy(currentOperationUser);
        restaurant.setUpdateBy(currentOperationUser);
        this.save(restaurant); // modify by liuxiang   先插入之后才会有商家信息
        restaurantExtService.saveDishUnit(restaurant);// 保持商家单位信息
        restaurantExtService.saveImgExt(restaurant,restaurantDTO);
        restaurantStaffService.updateStaffRestaurantId(restaurant.getId(),restaurantStaff.getId());
        ContextHolder.registerRestaurant(restaurant);
    }
    @Transactional
    protected void updateCurrentUserRestaurant(RestaurantDTO restaurantDTO, RestaurantStaff restaurantStaff) {
        Restaurant restaurant=new Restaurant();
        BeanUtils.copyProperties(restaurantDTO,restaurant);
        restaurant.setId(restaurantStaff.getRestaurantId());
        User currentOperationUser = RestaurantSupport.getCurrentOperationUser();
        restaurant.setUpdateBy(currentOperationUser);
        //TODO 调用翻译接口
        this.updateSelective(restaurant);
        restaurantExtService.resaveImgExt(restaurant,restaurantDTO);
    }
}
